/*******************************************************************************
 * Copyright (c) 2009-2011 ETCORE Labs. All rights reserved. 
 * 
 * This software is the confidential and proprietary information of ETCORE Labs.
 * You shall not disclose such confidential information and shall use it
 * only in accordance with the terms of the license agreement you entered into
 * with ETCORE Labs. 
 * 
 * Support can be obtained from support forums at:  http://www.etcore.com/
 * /
 *******************************************************************************/
package etcore.webapp.grouptalk.common.filter;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import etcore.webapp.grouptalk.common.config.Config;
import etcore.webapp.grouptalk.common.config.Messages;
import etcore.webapp.grouptalk.common.config.PageConstants;
import etcore.webapp.grouptalk.util.SymmetricEncryptionUtil;
import etcore.webapp.grouptalk.util.Symbols;
import java.io.IOException;
import java.util.Locale;

/**
 * This filter is used to parse an encrypted URL
 * 
 * @author ETCORE GroupTalk Developers
 * 
 */
public class UrlFilter implements Filter {

	public void destroy() {

	}

	public void doFilter(ServletRequest servletRequest,
			ServletResponse servletResponse, FilterChain chain)
			throws IOException, ServletException {

		HttpServletRequest request = (HttpServletRequest) servletRequest;

		HttpServletResponse response = (HttpServletResponse) servletResponse;
		HttpSession session = request.getSession(true);
		String targetURL = request.getRequestURI();

		// language
		String lang = request.getParameter(Messages.LANG);
		if (Messages.LANG_EN.equals(lang) || Messages.LANG_LOCAL.equals(lang))
			session.setAttribute(Messages.LANG, lang);

		String session_lang = (String) session.getAttribute(Messages.LANG);

		if (session_lang == null) {
			String default_lang = Config.getProperty(Config.DEFAULT_LANG);
			if (Messages.LANG_AUTO.equalsIgnoreCase(default_lang)) {
				if (Locale.ENGLISH.getLanguage().equals(
						request.getLocale().getLanguage()))

					default_lang = Messages.LANG_EN;
				else if (request.getLocale().getLanguage()
						.equals(Config.getProperty(Config.LOCAL_LANG)))

					default_lang = Messages.LANG_LOCAL;
				else
					default_lang = Messages.LANG_EN;

			}
			if (default_lang == null)
				default_lang = Messages.LANG_LOCAL;
			session.setAttribute(Messages.LANG, default_lang);
		}

		// url postfix
		String postfix = Config.getProperty(Config.POSTFIX);
		int index1 = targetURL.indexOf(postfix);
		int index2 = targetURL.indexOf("/~");
		if (postfix != null && index1 != -1 && index2 == -1) {

			request.getRequestDispatcher(
					targetURL.replace(postfix,
							PageConstants.PAGE_ACTION_POSTFIX)).forward(
					request, response);

			return;

		}
		if (postfix != null && index2 != -1) {

			String encoded = targetURL.substring(index2 + 2);
			if (encoded.endsWith(Symbols.SLASH))
				encoded = encoded.substring(0, encoded.length() - 1);
			String decoded = SymmetricEncryptionUtil.getDecryptedStr(encoded);
			String newUrl = targetURL.substring(0, index2).replace(postfix,
					PageConstants.PAGE_ACTION_POSTFIX)
					+ decoded;
			if (newUrl.indexOf(PageConstants.PAGE_ACTION_POSTFIX) == -1)
				response.sendRedirect(newUrl);
			else
				request.getRequestDispatcher(newUrl).forward(request, response);
			return;

		}

		chain.doFilter(request, response);

	}

	public boolean jumpAway() {
		return true;
	}

	public void init(FilterConfig filterConfig) throws ServletException {

	}

}
